package com.lzc.springcloud.msconsumeruser.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.lzc.springcloud.msconsumeruser.entity.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;

@RestController
public class UserController {
	
	@Autowired
	private RestTemplate restTemplate;
	
	@GetMapping("/getUser/{id}")
	@HystrixCommand(fallbackMethod = "getUserFallback", 
		commandProperties = {
			@HystrixProperty(name = "execution.isolation.strategy", value = "SEMAPHORE") 
		}
	)
	public User getUser(@PathVariable Integer id) {
		User user = this.restTemplate.getForObject("http://ms-eureka-provider-user-client/getUser/" + id, User.class);
		return user;
	}
	
	
	public User getUserFallback(Integer id) {
		User user = new User();
		user.setId(0);
		user.setName("失败了");
		return user;
	}
}
